﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 学生管理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示学生列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowStudents(object sender, EventArgs e)
        {
            StudentsDataContext sd = new StudentsDataContext();
            var q = from stus in sd.Students select stus;

            var q2 = from stus in sd.Students
                     from scores in sd.Scores
                     where stus.ScoreID == scores.ScoreID
                     select new
                     {
                         stus.Name,
                         stus.Age,
                         scores.Math,
                         scores.English,
                         scores.Chinese
                     };


            StringBuilder sb = new StringBuilder();

            foreach(var item in q2)
            {

                string str = string.Format("姓名:{0},年龄:{1},语文:{2},数学:{3},英语:{4}", 
                    item.Name,item.Age,item.Chinese,item.Math,item.English);
                sb.Append(str).Append("\r\n");

            }

            textBox1.Text = sb.ToString();




        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InitForm(object sender, EventArgs e)
        {
            StudentsDataContext sd = new StudentsDataContext();
            var q = from stus in sd.Students select stus;

            var q2 = from stus in sd.Students
                     from scores in sd.Scores
                     where stus.ScoreID == scores.ScoreID
                     select new
                     {
                         stus.Name,
                         stus.Age,
                         scores.Math,
                         scores.English,
                         scores.Chinese
                     };


            StringBuilder sb = new StringBuilder();

            foreach (var item in q2)
            {

                string str = string.Format("姓名:{0},年龄:{1},语文:{2},数学:{3},英语:{4}",
                    item.Name, item.Age, item.Chinese, item.Math, item.English);
                sb.Append(str).Append("\r\n");

            }

            textBox1.Text = sb.ToString();
        }

        /// <summary>
        /// 新增学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddStu(object sender, EventArgs e)
        {
            StudentsDataContext sd = new StudentsDataContext();
            
            //System.Data.Common.DbTransaction transaction = sd.Transaction;

            Students stu = new Students();
            stu.ID = 4;
            stu.Name = "王五";
            stu.Age = 45;
            //stu.ScoreID = 3;

            Scores scores = new Scores();
            scores.ScoreID = 4;
            scores.Chinese = 67;
            scores.Math = 67;
            scores.English = 126;

            stu.Scores = scores;

            sd.Students.InsertOnSubmit(stu);

            sd.SubmitChanges();
            //transaction.Commit();
        }
    }


    
}
